package com.meaningful.design_pattern.composite_pattern.calculator;

public class Calc {

    public static void main(String[] args) {
        Expression addExpression1 = new AddExpression(new NumberExpression(1), new NumberExpression(1));
        Expression addExpression2 = new AddExpression(new NumberExpression(2), new NumberExpression(3));
        Expression lastExpression = new AddExpression(addExpression1, addExpression2);
        System.out.println(lastExpression.getValue());
    }

    interface Expression {

        int getValue();

    }

    // - `NumberExpression` 和 `AddExpression` 是非静态的内部类。这意味着它们依赖于外部类 `Calc` 的实例。
    // 然而，在这种情况下，它们并不需要访问外部类的任何成员，因此可以将它们声明为 `static` 内部类，以减少不必要的依赖。
    static class NumberExpression implements Expression {

        final int value;

        public NumberExpression(int value) {
            this.value = value;
        }

        @Override
        public int getValue() {
            return value;
        }
    }

    static class AddExpression implements Expression {

        Expression left;

        Expression right;

        public AddExpression(Expression left, Expression right) {
            this.left = left;
            this.right = right;
        }

        @Override
        public int getValue() {
            return left.getValue() + right.getValue();
        }
    }

}
